package cn.sciento.farm.devicemanager.gateway.lorawan.grpc.impl;

import cn.sciento.farm.devicemanager.gateway.lorawan.config.JwtManager;
import cn.sciento.farm.devicemanager.gateway.lorawan.grpc.ApplicationService;
import cn.sciento.farm.devicemanager.gateway.lorawan.grpc.GrpcConstant;
import cn.sciento.farm.devicemanager.gateway.lorawan.grpc.JwtCallCredential;
import cn.sciento.farm.devicemanager.lorawan.ApplicationServiceGrpc;
import cn.sciento.farm.devicemanager.lorawan.ListApplicationRequest;
import cn.sciento.farm.devicemanager.lorawan.ListApplicationResponse;
import io.grpc.Channel;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author wumu
 * @version V1.0
 * @date 9/14/18
 * @time 2:31 PM
 */
@Service
public class ApplicationServiceImpl implements ApplicationService {

    @GrpcClient(GrpcConstant.GRPC_NAME)
    private Channel serverChannel;


    @Autowired
    private JwtManager jwtManager;


    static String jwtToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJsb3JhLWFwcC1zZXJ2ZXIiLCJleHAiOjE1MzY5OTM5OTksImlzcyI6ImxvcmEtYXBwLXNlcnZlciIsIm5iZiI6MTUzNjkwNzU5OSwic3ViIjoidXNlciIsInVzZXJuYW1lIjoiYWRtaW4ifQ.H36LXl_KrlJRkNXqSZZva4Jgxw30Z9OGhpOuS7F8s9o";


    @Override
    public ListApplicationResponse list(ListApplicationRequest request) {
        JwtCallCredential callCredential = jwtManager.getToken();
        ApplicationServiceGrpc.ApplicationServiceBlockingStub applicationServiceStub = ApplicationServiceGrpc.newBlockingStub(serverChannel).withCallCredentials(callCredential);
        ListApplicationResponse applicationResponse = applicationServiceStub.list(request);
        return applicationResponse;
    }
}
